package gw.com.android.ui.news;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gwtsz.gts2.hx.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.AppContances;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.terminal.AppTerminal;
import gw.com.android.utils.ColorThemeUtil;
import www.com.library.app.Logger;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.view.RecyclerClickListener;

/**
 * Created by reeta.zou on 2016/7/13.
 *
 */
public class NewsItemAdapter extends RecyclerView.Adapter{
    /** 左边菜单栏适配器数据对象 */
    private Context mContext;
    private DataItemResult mArray;
    private LayoutInflater mInflater = null;
    private RecyclerClickListener mClickListener = null;
    private ArrayList<Integer> mMarkList = null;

    public NewsItemAdapter(Activity mActivity, String mType, RecyclerClickListener mCallback) {
        mInflater = mActivity.getLayoutInflater();
        mClickListener = mCallback;
        mArray = new DataItemResult();
        mContext = mActivity;
        mMarkList = new ArrayList<Integer>();
        AppTerminal.instance().getNewsMarkReads(AppContances.MARK_NEWS_TYPE, mType, mMarkList);
        Logger.e(mMarkList.toString());
    }

    /**
     * 往后追加新的标识
     * @param mDataID
     */
    public void addMark(int mDataID){
        mMarkList.add(mDataID);
    }

    /**
     * 返回当前是否已读，true标识已读，false标识未读
     * @param mDataID
     * @return
     */
    private boolean getMark(int mDataID){
        if(mMarkList != null && mMarkList.size() > 0){
            for (int i = 0; i < mMarkList.size(); i++){
                if(mMarkList.get(i) == mDataID){
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 下拉刷新追加数据
     * @param mResult
     */
    public void addData(DataItemResult mResult){
        mArray.appendItems(mResult);
        notifyDataSetChanged();
    }

    /**
     * 上拉刷新替换新数据
     * @param mResult
     */
    public void replaceData(DataItemResult mResult){
        mArray.clear();
        mArray.appendItems(mResult);
        notifyDataSetChanged();
    }

    /**
     * 根据position返回界面item数据对象
     * @param position
     * @return
     */
    public DataItemDetail getItem(int position) {
        if (null != mArray) {
            if(position >= 0 && position < mArray.getDataCount()){
                return mArray.getItem(position);
            }
        }
        return null;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.list_item_news, parent,false);
        return new ItemHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemHolder mHolder = (ItemHolder) holder;
        DataItemDetail mItem = getItem(position);
        if(null != mItem){
            mHolder.itemView.setTag(position);
            mHolder.mTextview.setText(mItem.getString("title"));
            mHolder.mTimeview.setText(mItem.getString("ctime"));

            if(getMark(mItem.getInt("id"))){
                mHolder.mTextview.setTextColor(ColorThemeUtil.instance().color_h);
            }else{
                mHolder.mTextview.setTextColor(ColorThemeUtil.instance().color_b);
            }

            //这里就是异步加载网络图片的地方
            String imgUrl = ConfigUtil.instance().getNewsImageUrl(ConfigType.HTML_NEWS_IMAGE_TAG, mItem.getString("id"));
//            Logger.e("imgURl == " + imgUrl);
            Glide.with(mContext).load(imgUrl)
                    .placeholder(R.mipmap.a_news_nopic)
                    .error(R.mipmap.a_news_nopic)
                    .into(mHolder.mImageView);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (null != mArray) {
            return mArray.getDataCount();
        }
        return 0;
    }

    /**
     * item视图控件对象
     *
     * @author Administrator
     *
     */
    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        ImageView mImageView;
        @BindView(R.id.tv_news_title)
        TextView mTextview;
        @BindView(R.id.tv_news_time)
        TextView mTimeview;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.item_layout)
        void onViewClick(){
            if(null != mClickListener){
                int position = (int) itemView.getTag();
                mClickListener.onClick(position, getItem(position));
            }
        }
    }
}
